package com.qqtech.qquan.homemaking.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.agent.context.AdminPage;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.community.model.CommunityBase;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.homemaking.enums.HomemakingBaseAuditStatusDictEnum;
import com.qqtech.qquan.homemaking.enums.HomemakingBaseConst;
import com.qqtech.qquan.homemaking.enums.HomemakingItemStatusDictEnum;
import com.qqtech.qquan.homemaking.enums.HomemakingTypeStatusDictEnum;
import com.qqtech.qquan.homemaking.model.HomemakingBase;
import com.qqtech.qquan.homemaking.model.HomemakingBaseQuery;
import com.qqtech.qquan.homemaking.model.HomemakingBaseVo;
import com.qqtech.qquan.homemaking.model.HomemakingItemVo;
import com.qqtech.qquan.homemaking.model.HomemakingPubitemVo;
import com.qqtech.qquan.homemaking.model.HomemakingTypeVo;
import com.qqtech.qquan.homemaking.service.HomemakingBaseService;
import com.qqtech.qquan.homemaking.service.HomemakingItemService;
import com.qqtech.qquan.homemaking.service.HomemakingPubitemService;
import com.qqtech.qquan.homemaking.service.HomemakingSysDictService;
import com.qqtech.qquan.homemaking.service.HomemakingTypeService;
import com.qqtech.qquan.sys.controller.BaseAgentController;

@Controller
@RequestMapping(value = "/homemaking/base")
public class HomemakingBaseController extends BaseAgentController {

	@Resource
	private HomemakingBaseService homemakingBaseService;

	@Resource
	private HomemakingTypeService homemakingTypeService;

	@Resource
	private HomemakingSysDictService homemakingSysDictService;
	
	@Resource
	private HomemakingItemService homemakingItemService;
	
	@Resource
	private HomemakingPubitemService homemakingPubitemService;
	@Resource
	private CommunityBaseService communityBaseService;

	@Override
	protected BaseService<HomemakingBase> getBaseService() {
		return homemakingBaseService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		List<HomemakingTypeVo> typeList = homemakingTypeService
				.findParent(HomemakingTypeStatusDictEnum.VALID.getCode());
		model.addAttribute("typeList", typeList);
		return "/homemaking/homemakingbase-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(HomemakingBaseQuery query,
			HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		query.setCommunityId(super.getCurrCommunityId());//当前后台用户的社区id
		if (query.getFirstTypeId() != null
				&& query.getTypeId() != null) {
			query.setFirstTypeId(null);
		}
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(),query.getSize(), new PageOrder(
				Direction.DESC, "id"));
		Page<HomemakingBaseVo> page = homemakingBaseService.queryPageListAdminByType(
				query, pageKit);
		if (page.getContent() != null) {
			for (HomemakingBaseVo temp : page.getContent()) {
				HomemakingTypeVo type = homemakingTypeService.getById(temp
						.getTypeId());
				temp.setTypeStr(type.getName());
				temp.setStatusStr(homemakingSysDictService.getNameCache(
						HomemakingBaseConst.DICTYPE_HOMEMAKING_BASE_STATUS,
						temp.getStatus().toString()));
				temp.setIsShowStr(homemakingSysDictService.getNameCache(
						HomemakingBaseConst.DICTYPE_HOMEMAKING_BASE_ISSHOW,
						temp.getIsShow().toString()));
			}
		}
		adminPage.setRows(page.getContent());
		//adminPage.setTotal(page.getNumberOfElements());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements()+""));
		return adminPage;
	}
	
	/**
	 * 待审核-家政
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list2", method = RequestMethod.GET)
	public String list2(Model model) {
		List<HomemakingTypeVo> typeList = homemakingTypeService
				.findParent(HomemakingTypeStatusDictEnum.VALID.getCode());
		model.addAttribute("typeList", typeList);
		return "/homemaking/homemakingbase-list2";
	}

	@RequestMapping(value = "/list2", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list2(HomemakingBaseQuery query,
			HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		query.setCommunityId(super.getCurrCommunityId());//当前后台用户的社区id
		if (query.getFirstTypeId() != null
				&& query.getTypeId() != null) {
			query.setFirstTypeId(null);
		}
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		query.setAuditStatus(HomemakingBaseAuditStatusDictEnum.WAIT.getCode());//待审核
		Pageable pageKit = new PageKit(query.getPage(),query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<HomemakingBaseVo> page = homemakingBaseService.queryPageListAdminByType(
				query, pageKit);
		if (page.getContent() != null) {
			for (HomemakingBaseVo temp : page.getContent()) {
				HomemakingTypeVo type = homemakingTypeService.getById(temp.getTypeId());
				temp.setTypeStr(type.getName());
				temp.setStatusStr(homemakingSysDictService.getNameCache(
						HomemakingBaseConst.DICTYPE_HOMEMAKING_BASE_STATUS,
						temp.getStatus().toString()));
				temp.setIsShowStr(homemakingSysDictService.getNameCache(
						HomemakingBaseConst.DICTYPE_HOMEMAKING_BASE_ISSHOW,
						temp.getIsShow().toString()));
			}
		}
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements()+""));
		return adminPage;
	}
	
	@RequestMapping(value = "/detail")
	public String detail(Integer id, Model model) {
		HomemakingBaseVo homemakingBaseVo = homemakingBaseService.getById(id);
		HomemakingTypeVo type = homemakingTypeService.getById(homemakingBaseVo
				.getTypeId());
		homemakingBaseVo.setTypeStr(type.getName());
		homemakingBaseVo.setStatusStr(homemakingSysDictService.getNameCache(
				HomemakingBaseConst.DICTYPE_HOMEMAKING_BASE_STATUS,
				homemakingBaseVo.getStatus().toString()));
		homemakingBaseVo.setIsShowStr(homemakingSysDictService.getNameCache(
				HomemakingBaseConst.DICTYPE_HOMEMAKING_BASE_ISSHOW,
				homemakingBaseVo.getIsShow().toString()));
		List<HomemakingItemVo> itemVoList = homemakingItemService.findByBaseId(
				homemakingBaseVo.getId(),
				HomemakingItemStatusDictEnum.NORMAL.getCode());
		for (HomemakingItemVo temp : itemVoList) {
			HomemakingPubitemVo pubitemVo = homemakingPubitemService
					.getById(temp.getItemId());
			temp.setPubitemVo(pubitemVo);
		}
		homemakingBaseVo.setHomemakingItemVo(itemVoList);
		CommunityBase community = communityBaseService.getById(homemakingBaseVo.getCommunityId());
		if (community != null) {
			homemakingBaseVo.setCommunityName(community.getName());
		}
		model.addAttribute("homemakingBaseVo", homemakingBaseVo);
		return "/homemaking/homemakingbase-detail";
	}
	
	/**
	 * 审核
	 * @param homemakingBase
	 * @return
	 */
	@RequestMapping(value = "/auditStatus")
	@ResponseBody
	public Result auditStatus(@RequestBody HomemakingBaseVo homemakingBase) {
		Result result = new Result();
		Integer auditStatus = homemakingBase.getAuditStatus();
		HomemakingBaseVo homemakingBaseVo = null;
		if (homemakingBase == null || homemakingBase.getId() == null || auditStatus == null) {
			result.addErr("参数不能为空");
		} else {
			homemakingBaseVo = homemakingBaseService.getById(homemakingBase.getId());
			if (homemakingBaseVo == null) {
				result.addErr("数据不存在");
			} else if (auditStatus.intValue() == homemakingBaseVo.getAuditStatus()) {
				result.addErr("状态没有变化");
			}
		}
		if (result.checkForm()) {
			HomemakingBase entity = new HomemakingBase(homemakingBase.getId());
			entity.setAuditStatus(auditStatus);
			entity.setAuditAt(TimeUtil.now());
			homemakingBaseService.updateById(entity);
		}
		return result;
	}
	/**
	 * 接单状态
	 * @param homemakingBase
	 * @return
	 */
	@RequestMapping(value = "/status")
	@ResponseBody
	public Result status(@RequestBody HomemakingBaseVo homemakingBase) {
		Result result = new Result();
		Integer status = homemakingBase.getStatus();
		HomemakingBaseVo homemakingBaseVo = null;
		if (homemakingBase == null || homemakingBase.getId() == null || status == null) {
			result.addErr("参数不能为空");
		} else {
			homemakingBaseVo = homemakingBaseService.getById(homemakingBase.getId());
			if (homemakingBaseVo == null) {
				result.addErr("数据不存在");
			} else if (status.intValue() == homemakingBaseVo.getStatus()) {
				result.addErr("状态没有变化");
			}
		}
		if (result.checkForm()) {
			HomemakingBase entity = new HomemakingBase(homemakingBase.getId());
			entity.setStatus(status);
			homemakingBaseService.updateById(entity);
		}
		return result;
	}
}
